package com.another.common.utils.common;

import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;

/**
 * BigDecimal工具类
 * @author zhengxin
 */
public class DecimalUtil {

    /**
     * 判断是否是数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){
        if(StringUtils.isBlank(str)){
            return false;
        }
        return str.matches(RegexConstant.UNSIGNED_NUMERIC);
    }

    /**
     * 加法
     * @param augend
     * @param addend
     * @return
     */
    public static String add(String augend, String addend){
        return new BigDecimal(augend).add(new BigDecimal(addend)).toString();
    }


    /**
     * 比较
     * @param val1
     * @param val2
     * @return val1-val2
     */
    public static int compareTo(String val1, String val2){
        return new BigDecimal(val1).compareTo(new BigDecimal(val2));
    }

    /**
     * 减法
     * @param subtracted
     * @param subtraction
     * @return
     */
    public static String subtract(String subtracted, String subtraction){
        return new BigDecimal(subtracted).subtract(new BigDecimal(subtraction)).toString();
    }

    /**
     * 除法
     * @param dividend
     * @param divisor
     * @return 默认保留6位小数
     */
    public static String divide(String dividend , String divisor){
        return divide(dividend, divisor, 6);
    }

    /**
     * 除法
     * @param dividend 被除数
     * @param divisor   除数
     * @param scale 小数点位数
     * @return
     */
    public static String divide(String dividend , String divisor, int scale){
        return new BigDecimal(dividend).divide(new BigDecimal(divisor), scale, BigDecimal.ROUND_HALF_UP).toString();
    }


    /**
     *  乘法
     * @param multiplicand 被乘数
     * @param multiplier    乘数
     * @return
     */
    public static String multiply(String multiplicand, String multiplier){
        return multiplyDecimal(multiplicand, multiplier).toString();
    }

    /**
     *  乘法
     * @param multiplicand 被乘数
     * @param multiplier    乘数
     * @return
     */
    public static BigDecimal multiplyDecimal(String multiplicand, String multiplier){
        return new BigDecimal(multiplicand).multiply(new BigDecimal(multiplier));
    }




    public static void main(String[] args){

        System.out.println(new BigDecimal(1).divide(new BigDecimal(3), 0, BigDecimal.ROUND_HALF_UP).toString());
        System.out.println(new BigDecimal(2).divide(new BigDecimal(3), 0, BigDecimal.ROUND_HALF_UP).toString());

        System.out.println(new BigDecimal(1).divide(new BigDecimal(3), 0, BigDecimal.ROUND_HALF_DOWN).toString());
        System.out.println(new BigDecimal(2).divide(new BigDecimal(3), 0, BigDecimal.ROUND_HALF_DOWN).toString());
    }


    public static BigDecimal nullAsZero(String num){
        if(null == num || num == ""){
            return BigDecimal.ZERO;
        }else {
            return new BigDecimal(num);
        }
    }

    public static BigDecimal nullAsZero(BigDecimal num){
        if(null == num){
            return BigDecimal.ZERO;
        }else {
            return num;
        }
    }
}
